package com.toutou.base.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.databinding.Bindable
import androidx.recyclerview.widget.RecyclerView
import com.toutou.base.BR
import com.toutou.base.R
import com.toutou.base.adapter.viewholder.BaseNormalViewHolder
import com.toutou.base.adapter.viewholder.EmptyViewHolder
import com.toutou.base.adapter.viewholder.FooterViewHolder
import com.toutou.base.adapter.viewholder.LoadingViewHolder


open class BaseEmptyAdapterParent : BaseRecyclerAdapter {
    protected var c: Context
    protected var noMore: Boolean = false //尾部
    protected var baseStaggeredDelegate: BaseStaggeredDelegate? = null
    var showEndTips: Boolean
    private var confirmData = false //确认数据加载完毕·影响是否展示缺省页

    fun setConfirmData(confirmData: Boolean) {
        this.confirmData = confirmData //确认数据加载完毕·影响是否展示缺省页(有需求在数据未加载完成的时候 不要显示缺省页)
        notifyChange(BR.data)//外部需要对data做判断的时候 可以通知到
        if (data.isEmpty()){
            notifyDataChangeAll()//当数据为空的时候，刷新一波adapter，助力显示缺省页。有时候不会走到setData,到此就结束了业务逻辑。 当有数据的时候不用在此刻刷新adapter，因为数据加载完毕会自动刷新adapter
        }
    }

    constructor(context: Context, normalAdapterDelegate: NormalAdapterDelegate) : super(
        normalAdapterDelegate
    ) {
        this.c = context
        this.showEndTips = true
    }

    constructor(
        context: Context,
        showEndTips: Boolean,
        normalAdapterDelegate: NormalAdapterDelegate
    ) : super(normalAdapterDelegate) {
        this.c = context
        this.showEndTips = showEndTips
    }

    override fun getItemViewType(position: Int): Int {
        return if (list.isEmpty()) {
            if(confirmData) -100 else -99
        } else if (noMore && isFooterItem(position)) {
            -200
        } else {
            super.getItemViewType(position)
        }
    }

    override fun getItemCount(): Int {
        return if (list.isEmpty()) {
            1
        } else if (noMore && showEndTips) {
            list.size + 1
        } else super.getItemCount()
    }

    protected open fun initEmptyInfo(): String {
        return "no_data"
    }
    protected open fun initEmptyPic(): Int {
        return R.drawable.icon_empty
    }
    protected open fun initEmptyHeight(): Int {
        return 200
    }

    protected open fun initLoadingViewHolder(parent: ViewGroup?): BaseNormalViewHolder<*, *> {
        return LoadingViewHolder(c, parent)
    }
    protected open fun initEmptyViewHolder(parent: ViewGroup?): BaseNormalViewHolder<*, *> {
        return EmptyViewHolder(c, parent)
    }

    protected open fun initFooterViewHolder(parent: ViewGroup?): BaseNormalViewHolder<*, *> {
        return FooterViewHolder(c, parent)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            -99 -> {initLoadingViewHolder(parent)}
            -100 -> {
                initEmptyViewHolder(parent)
            }
            -200 -> {
                initFooterViewHolder(parent)
            }
            else -> super.onCreateViewHolder(parent, viewType)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (baseStaggeredDelegate != null) {
            baseStaggeredDelegate!!.handleLayoutIfStaggeredGridLayout(holder, position)
        }
        when (getItemViewType(position)) {
            -99 -> {}
            -100 -> if (holder is EmptyViewHolder) {
                holder.bindData(initEmptyInfo(), initEmptyPic(), initEmptyHeight())
            }

            -200 -> {}
            else ->  super.onBindViewHolder(holder, position)
        }
    }

    fun setNoMoreNotify(noMore: Boolean) {
        this.noMore = noMore
        notifyChange(BR.noMore)
    }

    @Bindable
    fun isNoMore(): Boolean {
        return noMore
    }

    interface BaseStaggeredDelegate {
        fun handleLayoutIfStaggeredGridLayout(holder: RecyclerView.ViewHolder?, position: Int)
    }

    fun notifyDataChangeAll() {
        notifyItemRangeChanged(0, itemCount)
    }

}